package com.woniuxy.handler;

import com.woniuxy.service.AccountBalanceService;
import com.work.framework.pojo.DepositDTO;
import com.work.framework.pojo.ResultDTO;
import com.work.framework.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: wanghao
 * @Date: 2019/9/17 20:18
 */
@Api(value = "处理用户帐号余额与拍卖有关的请求",tags = "竞拍押金")
@RestController
@RequestMapping("/balance")
public class AccountBalanceHandler {

    @Resource
    private AccountBalanceService accountBalanceService;

    @ApiOperation(value = "冻结押金")
    @PutMapping("/freeze/{aid}")
    public ResultDTO freezeDeposit(@PathVariable Integer aid) {
        //数据验证
        if (aid == null || aid == 0) {
            return null;
        }
        //调用server冻结押金
        Boolean isFreezeOk = accountBalanceService.freezeDeposit(aid);
        ResultDTO resultDTO = new ResultDTO();

        if (isFreezeOk) {
            resultDTO.setCode(200);
            resultDTO.setMessage("押金冻结成功");
            return resultDTO;
        } else {
            resultDTO.setCode(500);
            resultDTO.setMessage("押金冻结失败");
            return resultDTO;
        }
    }

    @ApiOperation(value = "解冻拍卖失败用户的押金")
    @PutMapping("/unfreeze/{aid}/{uid}")
    public ResultDTO unfreezeDeposit(@PathVariable Integer aid,@PathVariable Integer uid) {
        //1.数据验证
        if (aid == null || uid == null || aid == 0 || uid == 0) {
            return null;
        }
        //2.调用service解冻用户的押金
        Boolean isUnreezeOk = accountBalanceService.unfreezeDeposit(aid,uid);
        //3.结果处理
        ResultDTO resultDTO = new ResultDTO();
        if (isUnreezeOk) {
            resultDTO.setCode(200);
            resultDTO.setMessage("押金冻结成功");
            return resultDTO;
        } else {
            resultDTO.setCode(500);
            resultDTO.setMessage("押金冻结失败");
            return resultDTO;
        }
    }

    @ApiOperation(value = "遍历用户的押金")
    @GetMapping("/selectDeposit")
    public List<DepositDTO> selectDeposit(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object userObject = session.getAttribute("user");
        if (userObject == null) {
            return null;
        }
        User user = (User) userObject;
        //2.调用service展示用户的可退的押金
        List<DepositDTO> depositDTOS = accountBalanceService.selectDeposit(user.getUid());
        //3.结果处理
        return depositDTOS;
    }

    @ApiOperation(value = "退用户的押金")
    @GetMapping("/returnDeposit/{aid}/{gid}")
    public String returnDeposit(@PathVariable("aid") Integer aid,@PathVariable("gid")Integer gid, HttpServletRequest request) {

        HttpSession session = request.getSession();
        Object userObject = session.getAttribute("user");
        if (userObject == null) {
            return "用户未登录";
        }
        User user = (User) userObject;
        //1.数据验证
        if (user == null || user.getUid() == 0 || aid == null || aid == 0 || gid == null || gid == 0) {
            return null;
        }
        //2.调用service用户退押金
        Boolean isReturnOk = accountBalanceService.returnDeposit(user.getUid(),aid,gid);
        //3.结果处理
        if (isReturnOk) {
            return "退押金成功";
        }
        return "退押金失败";
    }


}
